package com.example.orderfood2;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.orderfood2.DatabaseManager.DatabaseManager;
import com.example.orderfood2.Repository.TimeTableRepository;
import com.example.orderfood2.entity.TimeTable;

public class AddCourse extends AppCompatActivity {
    //    获取各个输入框
    private EditText studentIdEditText,
            firstClassEditText, secondClassEditText,
            thirdClassEditText, fourthClassEditText,
            fifthClassEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TimeTableRepository timeTableRepository = DatabaseManager.getInstance(this).getTimeTableRepository();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_course);
//        获取跳转传来的id,
        Integer studentId = Integer.parseInt(getIntent().getStringExtra("id"));
//        给studentIdEditText绑定studentId
//        使用弹窗打印studentId
//        Toast.makeText(this, "!!" + studentId, Toast.LENGTH_SHORT).show();

        studentIdEditText = findViewById(R.id.et_student_id);
        studentIdEditText.setText(String.valueOf(studentId));
//        把输入框的值赋值给各变量
        firstClassEditText = findViewById(R.id.et_first_class);
        secondClassEditText = findViewById(R.id.et_second_class);
        thirdClassEditText = findViewById(R.id.et_third_class);
        fourthClassEditText = findViewById(R.id.et_fourth_class);
        fifthClassEditText = findViewById(R.id.et_fifth_class);
        TimeTable timeTable1 = timeTableRepository.queryTimeTableByStudentId(studentId);
//        进行数据回显
        if(timeTable1!=null){
            firstClassEditText.setText(timeTable1.getFirstClass());
            secondClassEditText.setText(timeTable1.getSecondClass());
            thirdClassEditText.setText(timeTable1.getThirdClass());
            fourthClassEditText.setText(timeTable1.getFourthClass());
            fifthClassEditText.setText(timeTable1.getFifthClass());
        }
        findViewById(R.id.btn_save).setOnClickListener(v -> {
            // 获取所有 EditText 的值
            String firstClass = firstClassEditText.getText().toString().trim();
            String secondClass = secondClassEditText.getText().toString().trim();
            String thirdClass = thirdClassEditText.getText().toString().trim();
            String fourthClass = fourthClassEditText.getText().toString().trim();
            String fifthClass = fifthClassEditText.getText().toString().trim();
            TimeTable timeTable = new TimeTable(studentId, firstClass, secondClass, thirdClass, fourthClass, fifthClass);
            long l = timeTableRepository.addTimeTable(timeTable);
            Toast.makeText(this, "课程添加成功"+l, Toast.LENGTH_SHORT).show();
        });
//        设置取消返回
        findViewById(R.id.btn_cancel).setOnClickListener(v -> finish());
    }
}
